package com.haida.translation_web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haida.translation_web.dao.TransRecordDao;
import com.haida.translation_web.pojo.TransRecordEntity;
import com.haida.translation_web.pojo.respone.PageUtils;
import com.haida.translation_web.pojo.respone.Query;
import com.haida.translation_web.pojo.respone.R;
import com.haida.translation_web.querier.Querier;
import com.haida.translation_web.service.TransRecordService;
import com.haida.translation_web.strategymodel.StrategyFactory;
import com.haida.translation_web.trans.AbstractTranslator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author LHJ_Jack
 * @Description TODO
 * @Date 2021/05/29 星期六 21:00
 */
@Service("transRecordService")
public class TransRecordServiceImpl extends ServiceImpl<TransRecordDao, TransRecordEntity> implements TransRecordService {

    @Autowired
    StrategyFactory strategyFactory;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<TransRecordEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (key != null && key != "") {
            wrapper.like("score", key)
                    .or()
                    .like("engine", key)
                    .or()
                    .like("target", key)
                    .or()
                    .like("originalEnglish", key)
                    .or()
                    .like("targetEnglish", key)
                    .or()
                    .like("create_time", key)
                    .or()
                    .like("original", key);

        }
        String startTime = (String) params.get("startTime");
        String endTime = (String) params.get("endTime");
        if (startTime != null && endTime != null) {
            wrapper.apply("create_time >= "+ "'"+startTime+"'" )
                    .apply("create_time <= " + "'"+endTime +"'");
        }
        IPage<TransRecordEntity> page = this.page(
                new Query<TransRecordEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public R trans(TransRecordEntity entity) {
        Querier<AbstractTranslator> querierTrans = new Querier<>();                   // 获取查询器

        // querierTrans.setParams(LANG.ZH, LANG.EN, "haha");    // 设置参数
        querierTrans.setParams(entity.getOriginal(), entity.getTarget(), entity.getOriginalenglish());    // 设置参数

        // querierTrans.attach(new GoogleTranslator());                                  // 向查询器中添加 Google 翻译器
        // querierTrans.attach(new BaiduTranslator());                                  // 向查询器中添加 Google 翻译器
        // querierTrans.attach(new YoudaoTranslator());                                  // 向查询器中添加 Google 翻译器
        if (!StrategyFactory.getStrategies().containsKey(entity.getEngine())) {
            return R.error("请选择正确的翻译引擎！");
        }
        AbstractTranslator strategy = StrategyFactory.getStrategy(entity.getEngine());
        querierTrans.attach(strategy);

        List<String> result = querierTrans.execute();
        return R.ok().put("trans", result);
    }
}
